package com.loop.unclelend.ui.fragment

import android.graphics.Color
import android.view.LayoutInflater
import android.view.View
import androidx.recyclerview.widget.LinearLayoutManager
import androidx.recyclerview.widget.RecyclerView
import com.loop.unclelend.R
import com.loop.unclelend.databinding.UiPagePermissionBinding
import com.loop.unclelend.pro.ProFragment
import com.loop.unclelend.utils.DensityUtils
import com.loop.unclelend.utils.adapter.UncleRecyclerViewAdapter
import com.loop.unclelend.utils.adapter.UncleViewHolderHelper
import com.loop.unclelend.utils.decoration.LinearSpacingItemDecoration
import org.json.JSONObject

/**
 * UncleLend
 * @Author wwdeng
 * @CreateTime: 2024/9/4 15:03
 * @Annotation：权限Page
 */
class Fm_PermissionPage(): ProFragment() {
    lateinit var binding: UiPagePermissionBinding
    private var data: JSONObject? = null

    //权限描述列表
    private var permissionAdapter: ItemAdapter? = null

    constructor(data: JSONObject): this(){
        this.data = data
    }

    override fun getUncleRootView(inflater: LayoutInflater): View {
        binding = UiPagePermissionBinding.inflate(inflater)
        return binding.root
    }

    override fun initUncleView() {
        rootViewBinding.layoutRoot.setBackgroundColor(Color.parseColor("#00000000"))

        initUncleRecyclerView()
    }

    override fun initUncleListener() {
    }

    override fun initUncleData() {
        val desList = arrayListOf<JSONObject>()
        if (data != null){
            val desArray = data!!.optJSONArray("msgList")
            if (desArray != null && desArray.length() > 0){
                for (i in 0..<desArray.length()){
                    desList.add(desArray.optJSONObject(i))
                }
            }
        }
        permissionAdapter!!.data = desList
    }

    /* 初始化列表 */
    private fun initUncleRecyclerView(){
//        binding.rvPermission.visibility = View.GONE
//        binding.rvPermission.setHasFixedSize(true)
//        binding.rvPermission.isNestedScrollingEnabled = false
//        binding.rvPermission.isFocusableInTouchMode = false //不需要焦点
        val layoutManager = LinearLayoutManager(context)
        binding.recyclerView.layoutManager = layoutManager
        val topDecoration = DensityUtils.dp2px(13f)
        binding.recyclerView.addItemDecoration(LinearSpacingItemDecoration(0, 0, 0, topDecoration))
        permissionAdapter = ItemAdapter(binding.recyclerView)
        permissionAdapter!!.setOnItemChildClickListener { _, _, position ->
            val item = permissionAdapter!!.getItem(position)
        }
        binding.recyclerView.adapter = permissionAdapter
    }

    class ItemAdapter(recyclerView: RecyclerView?) : UncleRecyclerViewAdapter<JSONObject>(recyclerView, R.layout.uncle_item_permission) {

        override fun setItemChildListener(helper: UncleViewHolderHelper, viewType: Int) {
            helper.setItemChildClickListener(R.id.itemLayout) //点击Item
        }

        override fun fillData(helper: UncleViewHolderHelper, position: Int, model: JSONObject) {
            helper.setText(R.id.txt_title,model.optString("title"))
            helper.setText(R.id.txt_des,model.optString("des"))
        }
    }
}